import tkinter as tk
import tkinter.ttk as ttk  # 导入 ttk 模块
import common
#import requests

class Student():
    __static_obj = None
    
    def __init__(self, right_panel) -> None:
        self.student_panel = tk.Frame(right_panel, borderwidth=1, relief=tk.SOLID, bg="red")
        #self.network_panel.place(x=0, y=0, width=100, height=200)
        #self.network_panel.pack(fill=tk.BOTH, expand=True)
        
        # 添加不可修改的标签
        self.student_label = tk.Label(self.student_panel, text="学生信息", font=("Arial", 14, "bold"))
        self.student_label.pack(side=tk.TOP, padx=10, pady=10)
        
        self.create_student_table()
        
        self.create_input_fields()
        
        self.student_panel.pack_forget()
        common.right_panel_register("student", self.student_panel)
        print("student panel init")
    
    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls.__static_obj:
            cls.__static_obj = cls(*args, **kwargs)
        return cls.__static_obj
    
    def show_student_panel(self):
        common.right_panel_show("student")
    
    def create_student_table(self):
        # 定义表格
        self.student_table = ttk.Treeview(self.student_panel, columns=("ID", "Name", "Age", "Grade"), show="headings")
        
        # 设置表格列
        self.student_table.heading("ID", text="ID")
        self.student_table.heading("Name", text="Name")
        self.student_table.heading("Age", text="Age")
        self.student_table.heading("Grade", text="Grade")
        
        # 设置表格列宽度
        self.student_table.column("ID", width=50)
        self.student_table.column("Name", width=100)
        self.student_table.column("Age", width=50)
        self.student_table.column("Grade", width=50)
        
        # 添加表格到面板
        self.student_table.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        
        # 示例数据
        self.student_table.insert("", tk.END, values=("1", "张三", "20", "大一"))
        self.student_table.insert("", tk.END, values=("2", "李四", "21", "大二"))
        
    def create_input_fields(self):
        # 创建输入框和标签
        self.name_label = tk.Label(self.student_panel, text="姓名:")
        self.name_label.pack(side=tk.LEFT, padx=10, pady=5)
        self.name_entry = tk.Entry(self.student_panel)
        self.name_entry.pack(side=tk.LEFT, padx=1, pady=5)
        
        self.age_label = tk.Label(self.student_panel, text="年龄:")
        self.age_label.pack(side=tk.LEFT, padx=10, pady=5)
        self.age_entry = tk.Entry(self.student_panel)
        self.age_entry.pack(side=tk.LEFT, padx=1, pady=5)
        
        self.grade_label = tk.Label(self.student_panel, text="年级:")
        self.grade_label.pack(side=tk.LEFT, padx=10, pady=5)
        self.grade_entry = tk.Entry(self.student_panel)
        self.grade_entry.pack(side=tk.LEFT, padx=1, pady=5)
        
        # 创建插入按钮
        self.insert_button = tk.Button(self.student_panel, text="插入", command=self.insert_student)
        self.insert_button.pack(side=tk.LEFT, padx=10, pady=10)
        
        # 创建删除按钮
        self.delete_button = tk.Button(self.student_panel, text="删除", command=self.delete_student)
        self.delete_button.pack(side=tk.LEFT, padx=10, pady=10)
    
    def insert_student(self):
        # 获取输入框中的数据
        name = self.name_entry.get()
        age = self.age_entry.get()
        grade = self.grade_entry.get()
        
        # 获取当前表格中的最大ID并加1作为新ID
        if self.student_table.get_children():
            last_id = int(self.student_table.item(self.student_table.get_children()[-1])["values"][0])
            new_id = last_id + 1
        else:
            new_id = 1
        
        # 插入数据到表格
        self.student_table.insert("", tk.END, values=(new_id, name, age, grade))
    
    def delete_student(self):
        # 获取选中的行
        selected_item = self.student_table.selection()
        
        if selected_item:
            # 删除选中的行
            self.student_table.delete(selected_item)
        else:
            print("请先选择要删除的行")

    
    
    